package com.idcheck.rechcustoms;


import android.os.AsyncTask;
import android.os.Environment;
import android.os.Message;


import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import utils.OtherUtil;


public class NetworkLogic {
    private static String DoMain = "http://app.yejianbao.com";
    //http://app.yejianbao.com   http://www.xmxbox.com
    private static NetworkLogic singletonNetworkLogic = new NetworkLogic();
    //将构造器设置为private禁止通过new进行实例化
    private NetworkLogic() {

    }
    public static NetworkLogic getInstance() {
        return singletonNetworkLogic;
    }
    public void postidforSendOpen(int comId,String phone,int adminId){
        try{
            JSONObject args = new JSONObject();
            args.put("phone",phone);
            args.put("companyId",comId);
            args.put("adminid",adminId);
            args.put("sn","1.0.1.20191204");
            args.put("ver","customs");



            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(DoMain+"/index/openGrid")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();


            Message message = new Message();
            message.what=1500;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);
        }catch (Exception e){
            Message message = new Message();
            message.what=1501;
            EventBus.getDefault().post(message);
        }
    }
    public void postidforSaveAuth(String phone, String password){
        try{
            JSONObject args = new JSONObject();
            args.put("mobile",phone);
            args.put("password",password);
            args.put("sn","1.0.1.20191204");
            args.put("ver","customs");



            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(DoMain+"/index/fileAdminid")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();


            Message message = new Message();
            message.what=1600;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);
        }catch (Exception e){
            Message message = new Message();
            message.what=1601;
            EventBus.getDefault().post(message);
        }
    }
    public void postidforSaveOpen(String phone){
        try{
            JSONObject args = new JSONObject();
            args.put("phone",phone);
            args.put("sn","1.0.1.20191204");
            args.put("ver","customs");



            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(DoMain+"/index/selfOpengrid")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();


            Message message = new Message();
            message.what=1700;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);
        }catch (Exception e){
            Message message = new Message();
            message.what=1701;
            EventBus.getDefault().post(message);
        }
    }

    public void postidforCompanyRecvAuth(String phone, String password){
        try{
            JSONObject args = new JSONObject();
            args.put("mobile",phone);
            args.put("sn","1.0.1.20191204");
            args.put("ver","customs");
            args.put("password",password);



            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(DoMain+"/index/fileAdminid")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();


            Message message = new Message();
            message.what=1300;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);
        }catch (Exception e){
            Message message = new Message();
            message.what=1301;
            EventBus.getDefault().post(message);
        }
    }
    public void postidforCompanyInfo(String companyid){
        try{
            JSONObject args = new JSONObject();
            args.put("companyNum",companyid);
//            args.put("sn",OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext()));
//            args.put("ver",OtherUtil.getInstance().getAppVersion());


//            String verInfo = OtherUtil.getInstance().getAppVersion() +" XB-"+ OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext());
            args.put("sn","1.0.1.20191204");
            args.put("ver","customs");



            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(DoMain+"/index/companyInfo")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();


            Message message = new Message();
            message.what=1400;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);
        }catch (Exception e){
            Message message = new Message();
            message.what=1401;
            EventBus.getDefault().post(message);
        }
    }
    public void postpasswordForRecvOpen(String password){
        try{
            JSONObject args = new JSONObject();
            args.put("code",password);
//            args.put("sn",OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext()));
//            args.put("ver",OtherUtil.getInstance().getAppVersion());


//            String verInfo = OtherUtil.getInstance().getAppVersion() +" XB-"+ OtherUtil.getInstance().getAppSerial(OtherUtil.getMainContext());
            args.put("sn","1.0.1.20191204");
            args.put("ver","customs");



            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(DoMain+"/index/getGrid")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();


            Message message = new Message();
            message.what=1000;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);

        }catch (Exception e) {
            Message message = new Message();
            message.what=1001;
            EventBus.getDefault().post(message);
        }

    }


    public void postorderList(String adminid) {
        try{
            JSONObject args = new JSONObject();
            args.put("adminid",adminid);
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(DoMain+"/index/yuyueList")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();


            Message message = new Message();
            message.what=1800;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);

        }catch (Exception e) {
            Message message = new Message();
            message.what=1001;
            EventBus.getDefault().post(message);
        }
    }

    public void postYuyueOpenGrid(String id, String adminid) {
        try{
            JSONObject args = new JSONObject();
            args.put("id",id);
            args.put("adminid",adminid);
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(DoMain+"/index/yuyueOpenGrid")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();


            Message message = new Message();
            message.what=1900;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);

        }catch (Exception e) {
            Message message = new Message();
            message.what=1001;
            EventBus.getDefault().post(message);
        }
    }

    public void postCompanyCabDetail(String adminid) {
        try{
            JSONObject args = new JSONObject();
            args.put("adminid",adminid);
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(DoMain+"/index/lanzhengList")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();


            Message message = new Message();
            message.what=2300;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);

        }catch (Exception e) {
            Message message = new Message();
            message.what=1001;
            EventBus.getDefault().post(message);
        }
    }

    public void postlanzhengOpenGrid(String cabinetGridId) {
        try{
            JSONObject args = new JSONObject();
            args.put("cabinetGridId",cabinetGridId);
            File file = new File(Environment.getExternalStorageDirectory(), "http_cache");
            int cacheSize = 10 * 1024 * 1024;
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .cache(new Cache(file, cacheSize));
            OkHttpClient client = builder.build();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, args.toString());
            Request request = new Request.Builder()
                    .url(DoMain+"/index/lanzhengOpenGrid")
                    .post(body)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();


            Message message = new Message();
            message.what=2400;
            message.obj=response.body().string();
            EventBus.getDefault().post(message);

        }catch (Exception e) {
            Message message = new Message();
            message.what=1001;
            EventBus.getDefault().post(message);
        }
    }
}
class FetchRecvOpenTask extends AsyncTask<Void,Void,Void> {
    String password;

    public FetchRecvOpenTask(String password) {
        this.password = password;
    }

    @Override
    protected Void doInBackground(Void... params) {

        NetworkLogic.getInstance().postpasswordForRecvOpen(password);
        return null;
    }

    @Override
    protected void onPostExecute(Void response) {

    }
}


class FetchSaveOpenTask extends AsyncTask<Void,Void,Void> {
    String phone;

    public FetchSaveOpenTask(String phone) {
        this.phone = phone;
    }

    @Override
    protected Void doInBackground(Void... params) {

        NetworkLogic.getInstance().postidforSaveOpen(phone);
        return null;
    }

    @Override
    protected void onPostExecute(Void response) {

    }
}

class FetchlanzhengOpenGridTask extends AsyncTask<Void,Void,Void> {
    String cabinetGridId;

    public FetchlanzhengOpenGridTask(String cabinetGridId) {
        this.cabinetGridId = cabinetGridId;
    }

    @Override
    protected Void doInBackground(Void... params) {

        NetworkLogic.getInstance().postlanzhengOpenGrid(cabinetGridId);
        return null;
    }

    @Override
    protected void onPostExecute(Void response) {

    }
}


class FetchCompanyCabDetailTask extends AsyncTask<Void,Void,Void> {
    String adminid;

    public FetchCompanyCabDetailTask(String adminid) {
        this.adminid = adminid;
    }

    @Override
    protected Void doInBackground(Void... params) {

        NetworkLogic.getInstance().postCompanyCabDetail(adminid);
        return null;
    }

    @Override
    protected void onPostExecute(Void response) {

    }
}

class FetchCompanyRecvAuthTask extends AsyncTask<Void,Void,Void> {
    String phone;
    String password;

    public FetchCompanyRecvAuthTask(String phone, String password) {
        this.phone = phone;
        this.password = password;
    }

    @Override
    protected Void doInBackground(Void... params) {

        NetworkLogic.getInstance().postidforCompanyRecvAuth(phone, password);
        return null;
    }

    @Override
    protected void onPostExecute(Void response) {

    }
}


class FetchSendOpenTask extends AsyncTask<Void,Void,Void> {
    int companyId;
    String companyPhone;
    int adminId;

    public FetchSendOpenTask(int comId,String phone ,int adminId) {
        this.companyPhone = phone;
        this.companyId = comId;
        this.adminId = adminId;
    }

    @Override
    protected Void doInBackground(Void... params) {

        NetworkLogic.getInstance().postidforSendOpen(companyId,companyPhone,adminId);
        return null;
    }

    @Override
    protected void onPostExecute(Void response) {

    }
}

class FetchSaveAuthTask extends AsyncTask<Void,Void,Void> {
    String phone;
    String password;

    public FetchSaveAuthTask(String phone, String password) {
        this.phone = phone;
        this.password = password;
    }

    @Override
    protected Void doInBackground(Void... params) {

        NetworkLogic.getInstance().postidforSaveAuth(phone, password);
        return null;
    }

    @Override
    protected void onPostExecute(Void response) {

    }
}
class FetchCompanyInfoTask extends AsyncTask<Void,Void,Void> {
    String companyid;

    public FetchCompanyInfoTask(String companyid) {
        this.companyid = companyid;
    }

    @Override
    protected Void doInBackground(Void... params) {

        NetworkLogic.getInstance().postidforCompanyInfo(companyid);
        return null;
    }

    @Override
    protected void onPostExecute(Void response) {

    }
}

class FetchOrderListTask extends AsyncTask<Void,Void,Void> {
    String adminid;

    public FetchOrderListTask(String adminid) {
        this.adminid = adminid;
    }

    @Override
    protected Void doInBackground(Void... params) {

        NetworkLogic.getInstance().postorderList(adminid);
        return null;
    }

    @Override
    protected void onPostExecute(Void response) {

    }
}

class FetchYuyueOpenGridTask extends AsyncTask<Void,Void,Void> {
    String id;
    String adminid;

    public FetchYuyueOpenGridTask(String id, String adminid) {
        this.adminid = adminid;
        this.id = id;
    }

    @Override
    protected Void doInBackground(Void... params) {

        NetworkLogic.getInstance().postYuyueOpenGrid(id, adminid);
        return null;
    }

    @Override
    protected void onPostExecute(Void response) {

    }
}

